﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Data.OleDb;
using System.Data.SqlClient;
using System.Linq;
using System.Security.Cryptography;
using System.Security.Principal;
using System.Text;
using System.Text.RegularExpressions;
using System.Web;

/// <summary>
/// Summary description for CMSUtilsAuthenticateUser
/// </summary>
public class CMSUtils
{
    // public static int phongBanID = 1;
    public CMSUtils()
    {
        //
        // TODO: Add constructor logic here
        //
    }
    public static void CheckUserAuthenticate(object sender)
    {

    }
    public static object InsertDataAndReturnValueSQL(string tableName, string[] column, object[] data)
    {
        string sampleQuery = "INSERT INTO ##TABLE## (##COLUMNS##) VALUES (##DATA##); SELECT scope_identity();";

        sampleQuery = sampleQuery.Replace("##TABLE##", tableName);

        //Xóa tất cả dữ liệu rỗng insert vào database
        CMSUtils.GetAndRemoveNullValue(ref column, ref data);

        //Nối các column
        string columns = "";
        for (int i = 0; i < column.Length; i++)
        {
            if (i == column.Length - 1)
            {
                columns += column[i];
            }
            else
            {
                columns += column[i] + ",";
            }
        }

        if (!string.IsNullOrEmpty(columns))
            sampleQuery = sampleQuery.Replace("##COLUMNS##", columns);
        else
            sampleQuery = sampleQuery.Replace("##COLUMNS##", "");

        //Khai báo parameter
        string param = "";
        for (int i = 0; i < column.Length; i++)
        {
            if (i == column.Length - 1)
            {
                param += "@" + column[i];
            }
            else
            {
                param += "@" + column[i] + ",";
            }
        }

        if (!string.IsNullOrEmpty(columns))
            sampleQuery = sampleQuery.Replace("##DATA##", param);
        else
            sampleQuery = sampleQuery.Replace("##DATA##", "");


        OleDbParameter[] par = new OleDbParameter[data.Length];
        for (int i = 0; i < data.Length; i++)
        {
            OleDbParameter singlePar = new OleDbParameter("@" + column[i], data[i]);
            par[i] = singlePar;
        }

        return OleDBHelper.ExecuteScalar(sampleQuery, par, false);
    }
    public static int InsertDataSQL(string tableName, string[] column, object[] data)
    {
        string sampleQuery = "INSERT INTO ##TABLE## (##COLUMNS##) VALUES (##DATA##)";

        sampleQuery = sampleQuery.Replace("##TABLE##", tableName);

        //Xóa tất cả dữ liệu rỗng insert vào database
        CMSUtils.GetAndRemoveNullValue(ref column, ref data);

        //Nối các column
        string columns = "";
        for (int i = 0; i < column.Length; i++)
        {
            if (i == column.Length - 1)
            {
                columns += column[i];
            }
            else
            {
                columns += column[i] + ",";
            }
        }

        if (!string.IsNullOrEmpty(columns))
            sampleQuery = sampleQuery.Replace("##COLUMNS##", columns);
        else
            sampleQuery = sampleQuery.Replace("##COLUMNS##", "");

        //Khai báo parameter
        string param = "";
        for (int i = 0; i < column.Length; i++)
        {
            if (i == column.Length - 1)
            {
                param += "@" + column[i];
            }
            else
            {
                param += "@" + column[i] + ",";
            }
        }

        if (!string.IsNullOrEmpty(columns))
            sampleQuery = sampleQuery.Replace("##DATA##", param);
        else
            sampleQuery = sampleQuery.Replace("##DATA##", "");


        OleDbParameter[] par = new OleDbParameter[data.Length];
        for (int i = 0; i < data.Length; i++)
        {
            OleDbParameter singlePar = new OleDbParameter("@" + column[i], data[i]);
            par[i] = singlePar;
        }

        return OleDBHelper.ExecuteNonQuery(sampleQuery, par, false);
    }
    public static int UpdateDataSQL(string MainColumn, string columnID, string tableName, string[] column, object[] data)
    {
        string sampleQuery = "UPDATE ##TABLE## SET ##COLUMNS## WHERE ##MAINCOLUMN##";

        sampleQuery = sampleQuery.Replace("##TABLE##", tableName);

        //Xóa cột có dữ liệu không thay đổi
        string cols = String.Join(",", column);
        DataTable dt = CMSUtils.GetDataSQL("", tableName, cols, String.Format("{0}='{1}'", MainColumn, columnID), "");
        if (dt.Rows.Count <= 0) return 1;
        CMSUtils.GetAndRemoveTheSameValue(ref column, ref data, ref dt);
        if (column.Length <= 0) return 1;

        //Nối các column
        string columns = "";
        for (int i = 0; i < column.Length; i++)
        {
            if (i == column.Length - 1)
            {
                columns += column[i] + "= @" + column[i];
            }
            else
            {
                columns += column[i] + "= @" + column[i] + ",";
            }
        }

        if (!string.IsNullOrEmpty(columns))
            sampleQuery = sampleQuery.Replace("##COLUMNS##", columns);
        else
            sampleQuery = sampleQuery.Replace("##COLUMNS##", "");


        if (!string.IsNullOrEmpty(MainColumn))
            sampleQuery = sampleQuery.Replace("##MAINCOLUMN##", MainColumn + "='" + columnID + "'");
        else
            sampleQuery = sampleQuery.Replace("##MAINCOLUMN##", "");

        OleDbParameter[] par = new OleDbParameter[data.Length];
        for (int i = 0; i < data.Length; i++)
        {
            OleDbParameter singlePar = new OleDbParameter("@" + column[i], data[i]);
            par[i] = singlePar;
        }

        return OleDBHelper.ExecuteNonQuery(sampleQuery, par, false);
    }
    public static int DeleteDataSQL(string tableName)
    {
        string sampleQuery = "DELETE FROM ##TABLE##";

        sampleQuery = sampleQuery.Replace("##TABLE##", tableName);

        return OleDBHelper.ExecuteNonQuery(sampleQuery);
    }
    public static int DeleteDataSQL(string MainColumn, string columnID, string tableName)
    {
        string sampleQuery = "DELETE FROM ##TABLE## WHERE ##MAINCOLUMN##";

        sampleQuery = sampleQuery.Replace("##TABLE##", tableName);

        //Nối các column
        if (!string.IsNullOrEmpty(MainColumn))
            sampleQuery = sampleQuery.Replace("##MAINCOLUMN##", MainColumn + "='" + columnID + "'");
        else
            sampleQuery = sampleQuery.Replace("##MAINCOLUMN##", "");

        int result = OleDBHelper.ExecuteNonQuery(sampleQuery);

        return result;
    }
    public static DataTable GetDataSQL(string top, string tableName, string columns, string where, string orderBy)
    {
        string sampleQuery = "SELECT ##TOPN## ##COLUMNS## FROM ##TABLE## ##WHERE## ##ORDERBY## ";

        if (!string.IsNullOrEmpty(top.ToString()))
            sampleQuery = sampleQuery.Replace("##TOPN##", "TOP " + top + " ");
        else
            sampleQuery = sampleQuery.Replace("##TOPN##", "");

        if (!string.IsNullOrEmpty(columns))
            sampleQuery = sampleQuery.Replace("##COLUMNS##", columns);
        else
            sampleQuery = sampleQuery.Replace("##COLUMNS##", "");

        sampleQuery = sampleQuery.Replace("##TABLE##", tableName);

        if (!string.IsNullOrEmpty(where))
            sampleQuery = sampleQuery.Replace("##WHERE##", " WHERE " + where + "");
        else
            sampleQuery = sampleQuery.Replace("##WHERE##", "");

        if (!string.IsNullOrEmpty(orderBy))
            sampleQuery = sampleQuery.Replace("##ORDERBY##", "ORDER BY " + orderBy);
        else
            sampleQuery = sampleQuery.Replace("##ORDERBY##", "");

        return OleDBHelper.ExecuteDataTable(sampleQuery);
    }
    public static object GetDataSQL(string tableName, string columns, string where, string orderBy)
    {
        string sampleQuery = "SELECT TOP 1 ##COLUMNS## FROM ##TABLE## ##WHERE## ##ORDERBY## ";
        if (!string.IsNullOrEmpty(columns))
            sampleQuery = sampleQuery.Replace("##COLUMNS##", columns);
        else
            sampleQuery = sampleQuery.Replace("##COLUMNS##", "");

        sampleQuery = sampleQuery.Replace("##TABLE##", tableName);

        if (!string.IsNullOrEmpty(where))
            sampleQuery = sampleQuery.Replace("##WHERE##", " WHERE " + where + "");
        else
            sampleQuery = sampleQuery.Replace("##WHERE##", "");

        if (!string.IsNullOrEmpty(orderBy))
            sampleQuery = sampleQuery.Replace("##ORDERBY##", " ORDER BY " + orderBy);
        else
            sampleQuery = sampleQuery.Replace("##ORDERBY##", "");

        return OleDBHelper.ExecuteScalar(sampleQuery);
    }
    public static string GetSafeString(string value)
    {
        if (!string.IsNullOrEmpty(value))
            return value.Replace("'", "")
                .Replace(";", "")
                .Replace("/", "")
                .Replace("\"", "")
                .Replace("%", "")
                .Replace("#", "")
                .Replace("?", "")
                .Replace("%", "")
                .Replace("$", "");
        else
            return "";
    }
    public static string GetPlainText(string s)
    {
        s = s.Replace("&nbsp;", " ");
        s = s.Replace("&lt;", "<");
        s = s.Replace("&gt;", ">");
        s = s.Replace("&amp;", "&");
        s = s.Replace("&quot;", "\"");
        s = Regex.Replace(s, @"\x20{2,}", " ");
        s = Regex.Replace(s, "<[^>]*>", "");
        return s.Trim();
    }
    public static String HashPassword(String password, String salt)
    {
        var combinedPassword = String.Concat(password, salt);
        var sha256 = new SHA256Managed();
        var bytes = UTF8Encoding.UTF8.GetBytes(combinedPassword);
        var hash = sha256.ComputeHash(bytes);
        return Convert.ToBase64String(hash);
    }
    public static Boolean AuthenticateUser(string userName, string passWord)
    {
        string userId = "-1";
        OleDbParameter[] pars = new OleDbParameter[2];
        OleDbParameter singleParU = new OleDbParameter("@Username", userName);
        OleDbParameter singleParP = new OleDbParameter("@Password", HashPassword(passWord, "sotuphap"));
        pars[0] = singleParU;
        pars[1] = singleParP;
        userId = OleDBHelper.ExecuteScalar("Validate_User", pars, true).ToString();
        switch (userId)
        {
            case "-1":
                return false;
            default:
                return true;
        }
    }
    public static Boolean CheckRoles(string pageName, string checkRole)
    {
        //DataTable tbAccount = CMSUtils.GetDataSQL("", "TB_TaiKhoan", "NhomTaiKhoanID", "TenTaiKhoan = '" + HttpContext.Current.User.Identity.Name + "'", "");

        //if (tbAccount.Rows.Count > 0)
        //{
        //    object pageID = CMSUtils.GetDataSQL("", "TB_Trang", "TrangID", String.Format("TenTrang like N'{0}'", GetSafeString(pageName)), "");

        //    if (pageID != null)
        //    {
        //        DataTable tbGroupAccountPage = CMSUtils.GetDataSQL("", "TB_NhomTaiKhoan_TB_Trang", "Xem,Tao,Sua,Xoa", "NhomTaiKhoanID = " + tbAccount.Rows[0]["NhomTaiKhoanID"].ToString() + " AND TrangID = N'" + pageID + "'", "");
        //        if (tbGroupAccountPage.Rows.Count > 0)
        //        {
        //            if (tbGroupAccountPage.Rows[0][checkRole] != null)
        //            {
        //                if (tbGroupAccountPage.Rows[0][checkRole].ToString() == "True")
        //                {
        //                    return true;
        //                }
        //            }
        //        }
        //    }
        //}
        return false;
    }
    public static string[] SplitString(string value, char split)
    {
        if (value != string.Empty)
        {
            string[] getValue = new string[3];
            if (value.Contains(split.ToString()))
            {
                getValue = value.Split(new Char[] { split });
            }
            else
            {
                getValue = new string[1];
                getValue.SetValue(value, 0);
            }
            return getValue;
        }
        return null;
    }
    public static bool IsNull(object value)
    {
        bool result = true;

        if (value != DBNull.Value && value != null && !String.IsNullOrEmpty(value.ToString()))
        {
            result = false;
        }

        return result;
    }
    public static bool IsNull(string value)
    {
        bool result = true;

        if (value != null && !String.IsNullOrEmpty(value.ToString()))
        {
            result = false;
        }

        return result;
    }
    public static void GetAndRemoveNullValue(ref string[] cols, ref object[] values)
    {
        string index = "";

        //Kiểm tra nếu null thì lưu index lại
        for (int i = 0; i < cols.Length; i++)
        {
            if (CMSUtils.IsNull(values[i]))
            {
                values[i] = DBNull.Value;
                index += i + ",";
            }
        }

        if (!String.IsNullOrEmpty(index))
        {
            index = index.Trim(',');
            string[] arrIndex = index.Split(',');
            //Xóa những cột không cần thiết ra khỏi array
            for (int i = arrIndex.Length - 1; i >= 0; i--)
            {
                cols = cols.Where((val, idx) => idx != int.Parse(arrIndex[i])).ToArray();
                values = values.Where((val, idx) => idx != int.Parse(arrIndex[i])).ToArray();
            }
        }
    }
    public static void GetAndRemoveTheSameValue(ref string[] cols, ref object[] values, ref object[] oldValues)
    {
        string index = "";

        //Kiểm tra nếu giống dữ liệu cũ thì xóa đi
        for (int i = 0; i < cols.Length; i++)
        {
            if (CMSUtils.IsNull(values[i])) values[i] = DBNull.Value;
            if (oldValues[i] == values[i])
            {
                index += i + ",";
            }
        }

        if (!String.IsNullOrEmpty(index))
        {
            index = index.Trim(',');
            string[] arrIndex = index.Split(',');
            //Xóa những cột không cần thiết ra khỏi array
            for (int i = arrIndex.Length - 1; i >= 0; i--)
            {
                oldValues = oldValues.Where((val, idx) => idx != int.Parse(arrIndex[i])).ToArray();
                cols = cols.Where((val, idx) => idx != int.Parse(arrIndex[i])).ToArray();
                values = values.Where((val, idx) => idx != int.Parse(arrIndex[i])).ToArray();
            }
        }
    }
    public static void GetAndRemoveTheSameValue(ref string[] cols, ref object[] values, ref DataTable oldDt)
    {
        string index = "";

        //Kiểm tra nếu giống dữ liệu cũ thì xóa đi
        for (int i = 0; i < cols.Length; i++)
        {
            if (CMSUtils.IsNull(values[i])) values[i] = DBNull.Value;
            if (oldDt.Rows[0][i].Equals(values[i]))
            {
                index += i + ",";
            }
        }

        if (!String.IsNullOrEmpty(index))
        {
            index = index.Trim(',');
            string[] arrIndex = index.Split(',');
            //Xóa những cột không cần thiết ra khỏi array
            for (int i = arrIndex.Length - 1; i >= 0; i--)
            {
                oldDt.Columns.Remove(cols[int.Parse(arrIndex[i])]);
                cols = cols.Where((val, idx) => idx != int.Parse(arrIndex[i])).ToArray();
                values = values.Where((val, idx) => idx != int.Parse(arrIndex[i])).ToArray();
            }
        }
    }
    #region [Validation]

    #region [IsNumber]
    public static bool IsNumber(object value, Type type)
    {
        bool result = false;

        if (!CMSUtils.IsNull(value))
        {
            switch (type.Name)
            {
                case "SByte":
                    SByte sSByte = 0;
                    if (SByte.TryParse(value.ToString(), out sSByte)) result = true;
                    break;
                case "Byte":
                    Byte bByte = 0;
                    if (Byte.TryParse(value.ToString(), out bByte)) result = true;
                    break;
                case "Int16":
                    Int16 iInt16 = 0;
                    if (Int16.TryParse(value.ToString(), out iInt16)) result = true;
                    break;
                case "Int32":
                    int iInt32 = 0;
                    if (int.TryParse(value.ToString(), out iInt32)) result = true;
                    break;
                case "Int64":
                    Int64 iInt64 = 0;
                    if (Int64.TryParse(value.ToString(), out iInt64)) result = true;
                    break;
                case "UInt16":
                    UInt16 uUint16 = 0;
                    if (UInt16.TryParse(value.ToString(), out uUint16)) result = true;
                    break;
                case "UInt32":
                    uint uInt = 0;
                    if (uint.TryParse(value.ToString(), out uInt)) result = true;
                    break;
                case "UInt64":
                    UInt64 uUInt64 = 0;
                    if (UInt64.TryParse(value.ToString(), out uUInt64)) result = true;
                    break;
                case "Double":
                    Double dDouble = 0;
                    if (Double.TryParse(value.ToString(), out dDouble)) result = true;
                    break;
                case "Single":
                    float fFloat = 0;
                    if (float.TryParse(value.ToString(), out fFloat)) result = true;
                    break;
                case "Decimal":
                    decimal dDecimal = 0;
                    if (decimal.TryParse(value.ToString(), out dDecimal)) result = true;
                    break;
            }
        }

        return result;
    }

    public static bool IsNumber(object value, out int number)
    {
        bool result = false;
        number = 0;
        object oNumber = 0;
        result = IsNumber(value, typeof(int), out oNumber);
        number = int.Parse(oNumber.ToString());

        return result;
    }

    public static bool IsNumber(object value, out Int16 number)
    {
        bool result = false;
        number = 0;
        object oNumber = 0;
        result = IsNumber(value, typeof(Int16), out oNumber);
        number = (Int16)oNumber;

        return result;
    }

    public static bool IsNumber(object value, out sbyte number)
    {
        bool result = false;
        number = 0;
        object oNumber = 0;
        result = IsNumber(value, typeof(sbyte), out oNumber);
        number = (sbyte)oNumber;

        return result;
    }

    public static bool IsNumber(object value, out byte number)
    {
        bool result = false;
        number = 0;
        object oNumber = 0;
        result = IsNumber(value, typeof(byte), out oNumber);
        number = (byte)oNumber;

        return result;
    }

    public static bool IsNumber(object value, out long number)
    {
        bool result = false;
        number = 0;
        object oNumber = 0;
        result = IsNumber(value, typeof(long), out oNumber);
        number = (long)oNumber;

        return result;
    }

    public static bool IsNumber(object value, out ulong number)
    {
        bool result = false;
        number = 0;
        object oNumber = 0;
        result = IsNumber(value, typeof(ulong), out oNumber);
        number = (ulong)oNumber;

        return result;
    }

    public static bool IsNumber(object value, out uint number)
    {
        bool result = false;
        number = 0;
        object oNumber = 0;
        result = IsNumber(value, typeof(uint), out oNumber);
        number = (uint)oNumber;

        return result;
    }

    public static bool IsNumber(object value, out UInt16 number)
    {
        bool result = false;
        number = 0;
        object oNumber = 0;
        result = IsNumber(value, typeof(UInt16), out oNumber);
        number = (UInt16)oNumber;

        return result;
    }

    public static bool IsNumber(object value, out float number)
    {
        bool result = false;
        number = 0;
        object oNumber = 0;
        result = IsNumber(value, typeof(float), out oNumber);
        number = (float)oNumber;

        return result;
    }
    public static bool IsNumber(object value, out double number)
    {
        bool result = false;
        number = 0;
        object oNumber = 0;
        result = IsNumber(value, typeof(double), out oNumber);
        number = (double)oNumber;

        return result;
    }
    public static bool IsNumber(object value, out decimal number)
    {
        bool result = false;
        number = 0;
        object oNumber = 0;
        result = IsNumber(value, typeof(decimal), out oNumber);
        number = (decimal)oNumber;

        return result;
    }
    public static bool IsNumber(object value, out sbyte? returnValue)
    {
        bool result = false;
        returnValue = null;

        if (!CMSUtils.IsNull(value))
        {
            sbyte temp = 0;
            if (sbyte.TryParse(value.ToString(), out temp)) result = true;
            returnValue = temp;
        }

        return result;
    }
    public static bool IsNumber(object value, out int? returnValue)
    {
        bool result = false;
        returnValue = null;

        if (!CMSUtils.IsNull(value))
        {
            int temp = 0;
            if (int.TryParse(value.ToString(), out temp)) result = true;
            returnValue = temp;
        }

        return result;
    }
    public static bool IsNumber(object value, out Int16? returnValue)
    {
        bool result = false;
        returnValue = null;

        if (!CMSUtils.IsNull(value))
        {
            Int16 temp = 0;
            if (Int16.TryParse(value.ToString(), out temp)) result = true;
            returnValue = temp;
        }

        return result;
    }
    public static bool IsNumber(object value, out long? returnValue)
    {
        bool result = false;
        returnValue = null;

        if (!CMSUtils.IsNull(value))
        {
            long temp = 0;
            if (long.TryParse(value.ToString(), out temp)) result = true;
            returnValue = temp;
        }

        return result;
    }
    public static bool IsNumber(object value, out uint? returnValue)
    {
        bool result = false;
        returnValue = null;

        if (!CMSUtils.IsNull(value))
        {
            uint temp = 0;
            if (uint.TryParse(value.ToString(), out temp)) result = true;
            returnValue = temp;
        }

        return result;
    }
    public static bool IsNumber(object value, out UInt16? returnValue)
    {
        bool result = false;
        returnValue = null;

        if (!CMSUtils.IsNull(value))
        {
            UInt16 temp = 0;
            if (UInt16.TryParse(value.ToString(), out temp)) result = true;
            returnValue = temp;
        }

        return result;
    }
    public static bool IsNumber(object value, out ulong? returnValue)
    {
        bool result = false;
        returnValue = null;

        if (!CMSUtils.IsNull(value))
        {
            ulong temp = 0;
            if (ulong.TryParse(value.ToString(), out temp)) result = true;
            returnValue = temp;
        }

        return result;
    }
    public static bool IsNumber(object value, out decimal? returnValue)
    {
        bool result = false;
        returnValue = null;

        if (!CMSUtils.IsNull(value))
        {
            decimal temp = 0;
            if (decimal.TryParse(value.ToString(), out temp)) result = true;
            returnValue = temp;
        }

        return result;
    }
    public static bool IsNumber(object value, out float? returnValue)
    {
        bool result = false;
        returnValue = null;

        if (!CMSUtils.IsNull(value))
        {
            float temp = 0;
            if (float.TryParse(value.ToString(), out temp)) result = true;
            returnValue = temp;
        }

        return result;
    }
    public static bool IsNumber(object value, out double? returnValue)
    {
        bool result = false;
        returnValue = null;

        if (!CMSUtils.IsNull(value))
        {
            double temp = 0;
            if (double.TryParse(value.ToString(), out temp)) result = true;
            returnValue = temp;
        }

        return result;
    }

    public static bool IsNumber(object value, Type type, out object number)
    {
        bool result = false;
        number = 0;

        if (!CMSUtils.IsNull(value))
        {
            switch (type.Name)
            {
                case "SByte":
                    SByte sSByte = 0;
                    if (SByte.TryParse(value.ToString(), out sSByte)) result = true;
                    number = sSByte;
                    break;
                case "Byte":
                    Byte bByte = 0;
                    if (Byte.TryParse(value.ToString(), out bByte)) result = true;
                    number = bByte;
                    break;
                case "Int16":
                    Int16 iInt16 = 0;
                    if (Int16.TryParse(value.ToString(), out iInt16)) result = true;
                    number = iInt16;
                    break;
                case "Int32":
                    int iInt32 = 0;
                    if (int.TryParse(value.ToString(), out iInt32)) result = true;
                    number = iInt32;
                    break;
                case "Int64":
                    Int64 iInt64 = 0;
                    if (Int64.TryParse(value.ToString(), out iInt64)) result = true;
                    number = iInt64;
                    break;
                case "UInt16":
                    UInt16 uUint16 = 0;
                    if (UInt16.TryParse(value.ToString(), out uUint16)) result = true;
                    number = uUint16;
                    break;
                case "UInt32":
                    uint uInt = 0;
                    if (uint.TryParse(value.ToString(), out uInt)) result = true;
                    number = uInt;
                    break;
                case "UInt64":
                    UInt64 uUInt64 = 0;
                    if (UInt64.TryParse(value.ToString(), out uUInt64)) result = true;
                    number = uUInt64;
                    break;
                case "Double":
                    Double dDouble = 0;
                    if (Double.TryParse(value.ToString(), out dDouble)) result = true;
                    number = dDouble;
                    break;
                case "Single":
                    float fFloat = 0;
                    if (float.TryParse(value.ToString(), out fFloat)) result = true;
                    number = fFloat;
                    break;
                case "Decimal":
                    decimal dDecimal = 0;
                    if (decimal.TryParse(value.ToString(), out dDecimal)) result = true;
                    number = dDecimal;
                    break;
            }
        }

        return result;
    }
    #endregion

    public static bool IsBoolean(object value)
    {
        bool result = false;

        if (!CMSUtils.IsNull(value))
        {
            bool bBoolean = false;
            if (bool.TryParse(value.ToString(), out bBoolean)) result = true;
        }

        return result;
    }

    public static bool IsBoolean(object value, out bool boolean)
    {
        bool result = false;
        boolean = false;

        if (!CMSUtils.IsNull(value))
        {
            bool bBoolean = false;
            if (bool.TryParse(value.ToString(), out bBoolean)) result = true;
            boolean = bBoolean;
        }

        return result;
    }

    public static bool IsBoolean(object value, out bool? boolean)
    {
        bool result = false;
        boolean = null;

        if (!CMSUtils.IsNull(value))
        {
            bool bBoolean = false;
            if (bool.TryParse(value.ToString(), out bBoolean)) result = true;
            boolean = bBoolean;
        }

        return result;
    }

    public static bool IsDateTime(object value)
    {
        bool result = false;

        if (!CMSUtils.IsNull(value))
        {
            DateTime dDateTime = DateTime.Now;
            if (DateTime.TryParse(value.ToString(), out dDateTime)) result = true;
        }

        return result;
    }

    public static bool IsDateTime(object value, out DateTime dateTime)
    {
        bool result = false;
        dateTime = DateTime.Now;

        if (!CMSUtils.IsNull(value))
        {
            DateTime dDateTime = DateTime.Now;
            if (DateTime.TryParse(value.ToString(), out dDateTime)) result = true;
            dateTime = dDateTime;
        }

        return result;
    }

    public static bool IsDateTime(object value, out DateTime? dateTime)
    {
        bool result = false;
        dateTime = null;

        if (!CMSUtils.IsNull(value))
        {
            DateTime dDateTime = DateTime.Now;
            if (DateTime.TryParse(value.ToString(), out dDateTime)) result = true;
            dateTime = dDateTime;
        }

        return result;
    }
    #endregion

    private string username;

    public string Username
    {
        get { return username; }
        set { username = value; }
    }
    private string password;

    public string Password
    {
        get { return password; }
        set { password = value; }
    }
    public CMSUtils(string username, string password)
    {
        this.username = username;
        this.password = password;
    }

    public CMSUtils login(string username )
    {
        CMSUtils tk = new CMSUtils();
        try
        {
            DataTable cmd = CMSUtils.GetDataSQL("", "dbo_TB_TaiKhoan", "*", String.Format("TenTaiKhoan='{0}'", username), "");
            tk.username = cmd.Rows[0]["TenTaiKhoan"].ToString();
        }
        catch
        {
            tk = null;
        }
        return tk;
    }

}
